package com.mmloo.manage.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mmloo.common.model.Goods;
import com.mmloo.common.page.Pager;
import com.mmloo.common.repository.CURDRepository;
import com.mmloo.common.util.DataTable;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by jessy on 2015/9/25.
 * 商品管理：添加商品，带分页带参数查找商品，更新商品，删除商品
 */
@Service
public class GoodsManageService {
    @Autowired
    CURDRepository curdRepository;

    public String  generateId(String tableName) throws Exception {
        ArrayList arrayList = new ArrayList();
        arrayList.add(tableName.getBytes());
        curdRepository.incr(arrayList);
        return curdRepository.get(arrayList);
    }

    public Goods addGoods(Goods goods) throws Exception {
        ArrayList<byte[]> hsetparames = new ArrayList<>();
        hsetparames.add(DataTable.GOODS_DETAIL_DATA.getBytes());
        String key = generateId(DataTable.GOODS_ID_SQUENCE);
        hsetparames.add(key.getBytes());
        goods.setId(key);
        //该商品的相关商品设置
        List list = findAllGoods("","",goods.getInventory_id());
        if(list!=null && list.size()>0){
            goods.setRelatedGood(list);
        }
        hsetparames.add(goods.toJson().getBytes());
        curdRepository.hset(hsetparames);
        return goods;
    }

    public List<Goods> findAllGoods(String keyword,String sale_type,String inventory) throws Exception {
        Map<String, String> categories = curdRepository.hscanAll(DataTable.GOODS_DETAIL_DATA);
        Set<Map.Entry<String, String>> entrySet = categories.entrySet();
        List<Goods> list = new ArrayList<>();
        if (!entrySet.isEmpty())
            for (Map.Entry<String, String> anEntrySet : entrySet) {
                String value = anEntrySet.getValue();
                //关键字判断
                //todo to check it
                if(!StringUtils.isEmpty(keyword)) {
                    if(!value.contains(keyword))
                       break;
                }
                    Goods goods = JSONObject.parseObject(value, Goods.class);
                System.out.println("<  -- list  img_urls ---> " + goods.getImage_urls());

                //商品类型
                //todo to check it
                if(!StringUtils.isEmpty(sale_type)){
                    if(!goods.getSale_type().equals(sale_type))
                        continue;
                }
                //商品库存
                if(!StringUtils.isEmpty(inventory)){
                    if(!goods.getInventory_id().equals(inventory)){
                        continue;
                    }
                }
                //有效对象
                    if (goods.getValid() == 1)
                        list.add(goods);
            }
        return list;
    }

    public Pager<Goods> getGoodsByPager(Pager<Goods> pager,String keyword,String sale_type) throws Exception {
        try {

        List<Goods> goodses = findAllGoods(keyword,sale_type,"");
        pager.setList(goodses);
        pager.setCount(goodses.size());
            return pager;
        }catch (Exception e){
            throw new DocumentException("获取商品数据失败");
        }

    }
    public Goods findGoods(String id) throws Exception {
        ArrayList<byte[]> parames = new ArrayList<>();
        parames.add(DataTable.GOODS_DETAIL_DATA.getBytes());
        parames.add(id.getBytes());
        String result = curdRepository.hget(parames);
        Goods goods = JSON.parseObject(result,Goods.class);
        return goods;
    }

    public void update(Goods goods) throws Exception {
        ArrayList<byte[]> parames = new ArrayList<>();
        parames.add(DataTable.GOODS_DETAIL_DATA.getBytes());
        parames.add(goods.getId().getBytes());
        parames.add(goods.toJson().getBytes());
        curdRepository.hset(parames);
    }

    public void delete(String id) throws Exception {
        Goods goods = findGoods(id);
        goods.setValid(0);
        update(goods);
    }
    public void test() throws Exception {
        Goods goods = findGoods("38");
        goods.setId("38");
         update(goods);
       goods = findGoods("40");
        goods.setId("40");
        update(goods);
        goods = findGoods("42");
        goods.setId("42");
        update(goods);
        goods = findGoods("44");
        goods.setId("44");
        update(goods);
        goods = findGoods("46");
        goods.setId("46");
        update(goods);
        goods = findGoods("48");
        goods.setId("48");
        update(goods);
        /*for(int i=1 ; i<=37 ; i++) {
            ArrayList<byte[]> parames = new ArrayList<>();
            parames.add(DataTable.GOODS_DETAIL_DATA.getBytes());
            parames.add((i + "").getBytes());
            curdRepository.hdel(parames);
        }
        System.out.println("。。。。delete success ");*/
    }
}
